import { createRouter, createWebHistory } from 'vue-router';
import { useUserInfoStore, useFlagStore, useFileStore } from '@/stores';
import { getUserInfoApi } from '@/apis/userInfo.js';
import { useDark, useToggle } from '@vueuse/core';
const isDark = useDark();
const toggleDark = useToggle(isDark);
// const pages = import.meta.glob("../views/**/page.js", {
//   eager: true,
//   import: "default",
// });

// const comps = import.meta.glob("../views/**/index.vue");

// const routes = Object.entries(pages).map(([path, meta]) => {
//   const compPath = path.replace("page.js", "index.vue");
//   path = path.replace("../views", "").replace("/page.js", "") || "/";
//   const name = path.split("/").filter(Boolean).join("-") || "index";
//   return {
//     path,
//     name,
//     component: comps[compPath],
//     meta,
//   };
// });

const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/indexi',
    name: 'indexi', // 首页
    component: () => import('@/views/indexi/index.vue')
  },
  {
    path: '/index',
    name: 'index', // 首页
    component: () => import('@/views/index/index.vue')
  },
  {
    path: '/plan',
    name: 'plan', // 定价方案
    component: () => import('@/views/plan/index.vue')
  },
  {
    path: '/loophole',
    name: 'loophole', // 报告漏洞
    component: () => import('@/views/loophole/index.vue')
  },
  {
    path: '/pay/:id(\\d+)?',
    name: 'pay', // 付款页
    component: () => import('@/views/pay/index.vue')
  },
  {
    path: '/help',
    name: 'help', // 帮助页
    component: () => import('@/views/help/index.vue')
  },
  {
    path: '/helpMenu',
    name: 'helpMenu',
    redirect: 'helpMenu/menu',
    component: () => import('@/views/helpMenu/index.vue'),
    children: [
      {
        path: 'menu',
        name: 'menu',
        component: () => import('@/views/helpMenu/menu.vue')
      },
      {
        path: 'detail/:articleId(\\d+)?',
        name: 'detail',
        component: () => import('@/views/helpMenu/detail.vue')
      }
    ]
  },
  {
    path: '/contactUs',
    name: 'contactUs', // 联系我们
    component: () => import('@/views/contactUs/index.vue')
  },
  {
    path: '/aboutUs',
    name: 'aboutUs', // 关于我们
    component: () => import('@/views/aboutUs/index.vue')
  },
  {
    path: '/privacyAgreement',
    name: 'privacyAgreement', //隐私协议
    component: () => import('@/views/privacyAgreement/index.vue')
  },
  {
    path: '/userAgreement',
    name: 'userAgreement', //用户协议
    component: () => import('@/views/userAgreement/index.vue')
  },
  {
    path: '/login',
    name: 'login', // 登录页
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/register',
    name: 'register', // 注册页
    component: () => import('@/views/register/index.vue')
  },
  {
    path: '/resetPassword',
    name: 'resetPassword', // 重置密码页
    component: () => import('@/views/resetPassword/index.vue')
  },
  {
    path: '/privacyPolicy/:id',
    name: 'privacyPolicy',
    component: () => import('@/views/privacyPolicy/index.vue')
  },

  {
    path: '/',
    component: () => import('@/views/platform/index.vue'),
    children: [
      {
        path: 'myFiles',
        name: 'myFiles', // 我的文件
        component: () => import('@/views/myFiles/index.vue')
      },
      {
        path: 'mySharedItems',
        name: 'mySharedItems', // 分享
        component: () => import('@/views/mySharedItems/index.vue')
      },
      {
        path: 'trash',
        name: 'trash', // 垃圾页
        component: () => import('@/views/trash/index.vue')
      },
      {
        path: '/mySmallfiles',
        name: 'mySmallfiles',
        component: () => import('@/views/mySmallfiles/index.vue')
      }
    ]
  },
  {
    path: '/userInfo',
    name: 'userInfo', // 个人信息
    component: () => import('@/views/userInfo/index.vue')
  },
  {
    path: '/shares/:id',
    name: 'shares', // 分享文件
    component: () => import('@/views/sharePage/index.vue')
  },
  {
    path: '/share/:id',
    name: 'share', // 分享页
    component: () => import('@/views/share/index.vue')
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

// 刷新用户信息
const refreshUserInfo = async () => {
  const userInfoStore = useUserInfoStore();
  const userInfoDetail = await getUserInfoApi();
  const { userInfo } = storeToRefs(userInfoStore); // 用户信息
  userInfo.value = userInfoDetail.data;
};
// 导航守卫
router.beforeEach(async (to, from) => {
  // 移动端我的文件页面打开导航栏
  const isMyFiles = /^\/myFiles/.test(to.fullPath);
  const flagStore = useFlagStore();
  if (isMyFiles) {
    flagStore.setMobileContentSidebarOpen(true);
  }
  const whiteList = [
    'index',
    'plan',
    'login',
    'register',
    'resetPassword',
    'help',
    'menu',
    'detail',
    'share',
    'contactUs',
    'indexi',
    'loophole',
    'aboutUs',
    'shares',
    'userAgreement',
    'privacyAgreement'
  ];
  if (whiteList.includes(to.name)) {
    toggleDark(false);
    return true;
  } else {
    refreshUserInfo();
    const userInfoStore = useUserInfoStore();
    const { okCloudDiskToken } = storeToRefs(userInfoStore); // 用户信息
    // 前端判断token过期
    if (!okCloudDiskToken.value) {
      return '/login';
    } else {
      const fileStore = useFileStore();
      fileStore.setdelFileList([]);
      fileStore.setVisible(1);
      return true;
    }
  }
});

router.afterEach(() => {
  // 跳转成功置顶
  window.scrollTo(0, 0);
});

export default router;
